<template>
  <Panel>
    <span slot="title">账号列表</span>
    <div slot="btn">
      <el-button type="primary" size="mini" @click="dialogVisible = true"
        >添加分类</el-button
      >
    </div>
    <div slot="main">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          width="100"
          label="序号"
        ></el-table-column>

        <el-table-column label="分类名称">
          <template slot-scope="scope">
            <span v-if="!scope.row.isEdit">{{ scope.row.cateName }}</span>
            <el-input
              v-else
              v-model="scope.row.cateName"
              placeholder="请输入内容"
            ></el-input>
          </template>
        </el-table-column>

        <el-table-column label="是否启用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">{{
              !scope.row.isEdit ? "编辑" : "完成"
            }}</el-button>
            <el-popconfirm
              title="确定删除吗?"
              class="ml10"
              @confirm="handleDelete(scope.row.id)"
            >
              <el-button size="mini" type="danger" slot="reference"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog title="添加分类" :visible.sync="dialogVisible">
        <el-form :model="eCate" size="small">
          <el-form-item label="分类名称" label-width="100px">
            <el-input v-model="eCate.cateName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="是否启用" label-width="100px">
            <el-switch
              v-model="eCate.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="aaaaa">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 分页 -->
      <div class="mt20">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[3, 5, 8, 10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </Panel>
</template>

<script>
import Panel from "@/components/panel.vue";
import { cateApi, addcateApi, delcateApi, editcateApi } from "@/api/goods";

export default {
  components: {
    Panel,
  },

  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,

      dialogVisible: false,

      eCate: {
        cateName: "",
        state: 0,
      },
      value: false,
    };
  },

  created() {
    this.getData();
  },

  methods: {
    async getData() {
      let res = await cateApi({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      });

      let { data, total } = res.data;

      data.map((v) => {
        v.state = v.state ? true : false;
        v.isEdit = false;
      });

      if (!data.length && this.currentPage != 1) {
        this.currentPage -= 1;
        this.getData();
      }

      this.tableData = data;
      this.total = total;
      // console.log(data);
    },

    //添加分类
    async aaaaa() {
      // console.log(1212);
      this.eCate.state = this.eCate.state ? 1 : 0;
      let res = await addcateApi(this.eCate);
      if (res.data.code == 0) {
        this.dialogVisible = false;
        this.eCate = {};
        this.getData();
      }
    },

    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },

    // 删除
    async handleDelete(id) {
      let res = await delcateApi({ id });

      if (res.data.code == 0) {
        this.getData();
      }
    },

    //编辑功能
    async handleEdit(row) {
      row.isEdit = !row.isEdit;
      // console.log(row);
      if (!row.isEdit) {
        row.state = row.state ? 1 : 0;
        let res = await editcateApi(row);
        if (res.data.code == 0) {
          this.getData();
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.mt20 {
  margin-top: 20px;
}
.ml10 {
  margin-left: 10px;
}
/deep/.el-dialog {
  width: 400px;
}
.el-input {
  width: 194px;
}
</style>